# ----------------------------------------------------------------------------
# CMake 最低要求版本指定
# ----------------------------------------------------------------------------

cmake_minimum_required(VERSION 3.10)



# ----------------------------------------------------------------------------
# gnu-arm 交叉编译工具链文件
# ----------------------------------------------------------------------------

set(CMAKE_TOOLCHAIN_FILE "${CMAKE_CURRENT_LIST_DIR}/config/STM32F407ZGTx_HAL/gnu-arm/cmake/gnu-arm_cm4.cmake"
    CACHE FILEPATH "CMake toolchain file path")



# ----------------------------------------------------------------------------
# pre-defined variables
# ----------------------------------------------------------------------------
#
#     - CUSTOM_PROJECT_NAME      # 工程名称
#
#     # 应用层相关
#     - CUSTOM_APPS_MACRO        # 应用层宏定义
#     - CUSTOM_APPS_INC          # 应用层头文件路径
#     - CUSTOM_APPS_SRCS         # 应用层源文件
#     - CUSTOM_APPS_COMPILE_OPTS # 应用层编译选项
#     - CUSTOM_APPS_LINK_OPTS    # 应用层链接选项
#     - CUSTOM_APPS_LIBS         # 应用层库文件
#
#     # HAL_Driver 库相关
#     - CUSTOM_DEPS_HAL_NAME    # HAL 库名称
#     - CUSTOM_DEPS_HAL_PATH    # HAL 库路径
#     - CUSTOM_DEPS_HAL_MACRO   # HAL 库依赖宏定义
#     - CUSTOM_DEPS_HAL_INC     # HAL 库依赖头文件
# ----------------------------------------------------------------------------

# 应用层相关
set(CUSTOM_PROJECT_NAME "apps")

set(CUSTOM_APPS_LINK_OPTS
    -T "${CMAKE_CURRENT_LIST_DIR}/config/STM32F407ZGTx_HAL/gnu-arm/STM32F407ZGTx_FLASH.ld"
    -u _printf_float)

set(CUSTOM_APPS_INC
    "${CMAKE_CURRENT_LIST_DIR}/apps/inc")

set(CUSTOM_APPS_SRCS
    "${CMAKE_CURRENT_LIST_DIR}/config/STM32F407ZGTx_HAL/gnu-arm/startup_stm32f407xx.s"
    "${CMAKE_CURRENT_LIST_DIR}/config/STM32F407ZGTx_HAL/src/system_stm32f4xx.c"
    "${CMAKE_CURRENT_LIST_DIR}/apps/stm32f4xx_hal_msp.c"
    "${CMAKE_CURRENT_LIST_DIR}/apps/main.c"
    "${CMAKE_CURRENT_LIST_DIR}/apps/bsp.c")


# HAL 库相关
set(CUSTOM_DEPS_HAL_NAME "STM32F4xx_HAL_Driver")

set(CUSTOM_DEPS_HAL_PATH "${CMAKE_CURRENT_LIST_DIR}/third_party/STM32F4xx_HAL_Driver")

set(CUSTOM_DEPS_HAL_INC  "${CMAKE_CURRENT_LIST_DIR}/config/STM32F407ZGTx_HAL/inc")

set(CUSTOM_DEPS_HAL_MACRO
    USE_HAL_DRIVER
    STM32F407xx)



# ----------------------------------------------------------------------------
# 构建类型 Debug (default) or Release -DCMAKE_BUILD_TYPE=Debug/Release
# ----------------------------------------------------------------------------

set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "CMake build type")



# ----------------------------------------------------------------------------
# compile_commands.json 文件生成
# ----------------------------------------------------------------------------

set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE BOOL "generate compile_commands.json")



# ----------------------------------------------------------------------------
# 工程创建
# ----------------------------------------------------------------------------

project(${CUSTOM_PROJECT_NAME} LANGUAGES C CXX ASM)



# ----------------------------------------------------------------------------
# HAL_Driver 库导入
# ----------------------------------------------------------------------------

if (NOT "${CUSTOM_DEPS_HAL_NAME}" STREQUAL "")

    set(HAL_DEPS_NAME "${CUSTOM_DEPS_HAL_NAME}_DEPS")
    add_library(${HAL_DEPS_NAME} INTERFACE)

    target_compile_definitions(${HAL_DEPS_NAME} INTERFACE ${CUSTOM_DEPS_HAL_MACRO})
    target_include_directories(${HAL_DEPS_NAME} INTERFACE ${CUSTOM_DEPS_HAL_INC})

    add_subdirectory(${CUSTOM_DEPS_HAL_PATH})

endif()



# ----------------------------------------------------------------------------
# 可执行目标创建
# ----------------------------------------------------------------------------

set(EXEC_TARGET_NAME "${PROJECT_NAME}.elf")
add_executable(${EXEC_TARGET_NAME})

# 目标文件输出路径
set_target_properties(${EXEC_TARGET_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${PROJECT_NAME}")

# 宏定义
target_compile_definitions(${EXEC_TARGET_NAME} PRIVATE ${CUSTOM_APPS_MACRO})

# 头文件
target_include_directories(${EXEC_TARGET_NAME} PRIVATE ${CUSTOM_APPS_INC})

# 源文件
target_sources(${EXEC_TARGET_NAME} PRIVATE ${CUSTOM_APPS_SRCS})

# 编译器选项
target_compile_options(${EXEC_TARGET_NAME} PRIVATE ${CUSTOM_APPS_COMPILE_OPTS})

# 链接器选项
target_link_options(${EXEC_TARGET_NAME}
PRIVATE
    -Wl,-Map=$<TARGET_FILE_DIR:${EXEC_TARGET_NAME}>/${PROJECT_NAME}.map
    ${CUSTOM_APPS_LINK_OPTS})

# 依赖库
target_link_libraries(${EXEC_TARGET_NAME}
PRIVATE
    ${CUSTOM_APPS_LIBS}
    ${CUSTOM_DEPS_HAL_NAME})



# ----------------------------------------------------------------------------
# 内存占用打印以及 hex/bin 文件生成
# ----------------------------------------------------------------------------

add_custom_command(TARGET ${EXEC_TARGET_NAME} POST_BUILD
    COMMAND ${CMAKE_SIZE} --format=sysv --radix=16 --total $<TARGET_FILE:${EXEC_TARGET_NAME}>
    COMMAND ${CMAKE_OBJCOPY} -O ihex $<TARGET_FILE:${EXEC_TARGET_NAME}> $<TARGET_FILE_DIR:${EXEC_TARGET_NAME}>/${PROJECT_NAME}.hex
    COMMAND ${CMAKE_OBJCOPY} -O binary -S $<TARGET_FILE:${EXEC_TARGET_NAME}> $<TARGET_FILE_DIR:${EXEC_TARGET_NAME}>/${PROJECT_NAME}.bin
)
